﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Data;

namespace DEMO3
{
    class DataProvider
    {
        private string _ConnectString = @"Data Source=MIN-PC\SQLEXPRESS;Initial Catalog=QL_RAPCHIEUPHIM;Integrated Security=True";
        private SqlConnection _cn;

        public DataProvider()
        {
            _cn = new SqlConnection(_ConnectString);            
        }
        public bool OpenConnect()
        {
            try
            {
                _cn.Open();
                DEMO3.Main.b_connection = true;
                return true;
            }
            catch
            {
                DEMO3.Main.b_connection = false;
                return false;
            }            
        }

        public bool CloseConnect()
        {
            try
            {
                _cn.Close();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool ExcuteNonQuery(string procname, SqlParameter[] arrparameter)
        {
            try
            {
                OpenConnect();
                SqlCommand cmd = _cn.CreateCommand();
                cmd.Connection = _cn;
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.CommandText = procname;
                if (arrparameter !=null)
                {
                    cmd.Parameters.AddRange(arrparameter);
                }
                cmd.ExecuteNonQuery();
                CloseConnect();
                return true;
            }
            catch
            {
                MessageBox.Show("Thao tác thực hiện không thành công!\nVui lòng kiểm tra lại!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                CloseConnect();
                return false;
            }          
        }

        public DataTable ExcuteQuery(string procname, SqlParameter[] arrparameter)
        {
            DataTable dt = new DataTable();
            try
            {
                SqlCommand cmd = _cn.CreateCommand();
                cmd.Connection = _cn;
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.CommandText = procname;
                if (arrparameter != null)
                {
                    cmd.Parameters.AddRange(arrparameter);
                }
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dt);                
            }
            catch
            {

            }
            CloseConnect();
            return dt;
        }

        public Object ExcuteScalar(string procname, SqlParameter[] arrparameter)
        {
            Object oj = new Object();
            try
            {
                SqlCommand cmd = _cn.CreateCommand();
                cmd.Connection = _cn;
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.CommandText = procname;
                if (arrparameter != null)
                {
                    cmd.Parameters.AddRange(arrparameter);
                }
                oj = cmd.ExecuteScalar();
            }
            catch
            {

            }
            CloseConnect();
            return oj;
        }
    }
}
